package com.es.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.elasticsearch.action.search.MultiSearchRequest;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.es.interfaces.DocInterface;
import com.es.util.DeQueryBuilder;
import com.es.util.PageData;

@RestController
@RequestMapping("/es")
public class TestController {

	@Autowired
	DocInterface DocInterface;

	@Autowired
	private RestHighLevelClient restHighLevelClient;

	@PostMapping("/insert")
	public Object getData(@RequestBody Map<String, Object> map) throws InterruptedException {
		DocInterface.insert(map.get("index").toString(), map.get("id").toString(), map);
		return "true";
	}

	@GetMapping("/del")
	public Object getData(String index, String id) throws InterruptedException {
		DocInterface.deleteBatch(index, Arrays.asList(id));
		return "true";
	}

	@PostMapping("/update")
	public Object update(@RequestBody Map<String, Object> map) throws InterruptedException {
		DocInterface.update(map.get("index").toString(), map.get("id").toString(), map);
		return "true";
	}

	@PostMapping("/test")
	public Object test() throws InterruptedException {
		Map<String, Object> where = new HashMap<>();

//		if (where != null && !where.isEmpty()) {
//			BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//			where.forEach((k, v) -> {
//				if (v instanceof Map) {
//					// 范围选择map 暂定时间
//					Map<String, Date> mapV = (Map<String, Date>) v;
//					if (mapV != null) {
//						boolQueryBuilder.must(QueryBuilders.rangeQuery(k).gte(format.format(mapV.get("start")))
//								.lt(format.format(mapV.get("end"))));
//					}
//				} else {
//					// 普通模糊匹配
//					boolQueryBuilder.must(QueryBuilders.wildcardQuery(k, v.toString()));
//				}
//			});
//			sourceBuilder.query(boolQueryBuilder);
//		}

		DeQueryBuilder  builder = DeQueryBuilder.buildQueryFactory();
		
		builder
//			   .queryTermBuilder("deilTime", "2021122000000")
//			   .queryTermsBuilder("ageLowerLimit", Arrays.asList(8,9,10))
//			   .queryRangeBuilder("deilTime", "2021121000000","2021122000000")
//				.queryFuzzyBuilder("messageId", "599469411173351411")
				.queryAllBuilder()
			   .search(null)
			   .queryPageBuilder(1, 2)
			   .querySortBuilder("infoKind", false);
		PageData<Map>  data = DocInterface.search("face", builder, Map.class);
		return data;
	}

}
